package com.dao.impl;

import com.dao.Pay;
import com.entity.allPrices;
import com.entity.partPrice;
import com.util.dbCRUD;
import com.util.nowTime;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import static com.util.Vessel.*;

public class PayImpl implements Pay {

    //其他支付方式
    @Override
    public int weChat(int cId) {

        String sql = "UPDATE orders\n" +
                "SET oStatus = '已支付'\n" +
                "WHERE cId = ?\n" +
                "AND oStatus = '已下单，待支付'";
        Object[] objects = new Object[1];
        objects[0] = cId;
        int num = payCRUD.update(sql,objects);
        return num;
    }

    //每个订单总价
    @Override
    public List<partPrice> aPrice(int cId) {
        String sql = "SELECT orders.oId,orders.oTime,SUM(Price) AS aPrice\n" +
                "FROM orders\n" +
                "RIGHT JOIN (\n" +
                "    SELECT od.oId,me.mId,me.mName,SUM(mNumber) Number,me.mPrice,tId,od.cId,oStatus,mNumber*mPrice Price\n" +
                "    FROM order_details AS od\n" +
                "             LEFT JOIN orders AS os ON od.oId=os.oId\n" +
                "             LEFT JOIN menus AS me ON me.mId = od.mId\n" +
                "    WHERE os.cId = ? AND oStatus = '已下单，待支付'\n" +
                "    GROUP BY me.mId, od.oId, me.mName, mNumber, me.mPrice, tId, od.cId, oStatus, mNumber*mPrice\n" +
                ")AS orders1 ON orders.oId = orders1.oId\n" +
                "WHERE orders1.oStatus = '已下单，待支付'\n" +
                "GROUP BY orders.oId";
        Object[] objects = new Object[1];
        objects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<partPrice> priceList = new ArrayList<>();
        try{
            while(resultSet.next()){
                partPrice price = new partPrice();
                price.setoId(resultSet.getInt("orders.oId"));
                price.setoTime(resultSet.getString("orders.oTime"));
                price.setaPrice(resultSet.getDouble("aPrice"));
                priceList.add(price);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return priceList;
    }

    //所有订单总价
    @Override
    public List<allPrices> allPrice(int cId) {
        String sql = "SELECT SUM(Price)AS allPrice\n" +
                "FROM orders\n" +
                "RIGHT JOIN (\n" +
                "    SELECT od.oId,me.mId,me.mName,SUM(mNumber) Number,me.mPrice,tId,od.cId,oStatus,mNumber*mPrice Price\n" +
                "    FROM order_details AS od\n" +
                "             LEFT JOIN orders AS os ON od.oId=os.oId\n" +
                "             LEFT JOIN menus AS me ON me.mId = od.mId\n" +
                "    WHERE os.cId = ? AND oStatus = '已下单，待支付'\n" +
                "    GROUP BY me.mId, od.oId, me.mName, mNumber, me.mPrice, tId, od.cId, oStatus, mNumber*mPrice\n" +
                ")AS orders1 ON orders.oId = orders1.oId\n" +
                "WHERE orders1.oStatus = '已下单，待支付'\n" +
                "GROUP BY orders.cId";
        Object[] objects = new Object[1];
        objects[0] = cId;
        ResultSet resultSet = orderCRUD.printAll(sql,objects);
        List<allPrices> priceList = new ArrayList<>();
        try{
            while(resultSet.next()){
                allPrices prices = new allPrices();
                prices.setAllPrice(resultSet.getDouble("allPrice"));
                priceList.add(prices);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return priceList;
    }

    //会员卡支付
    @Override
    public int vipPay(int cId, String vipPassword,double allPrices) {
        int num = 0;
        double balance = vipImpl.vipBalance(cId,vipPassword);
        if (balance > allPrices){
            String sql = "UPDATE clients\n" +
                    "SET cBalance = cBalance-0.9*?\n" +
                    "WHERE cId = ?\n" +
                    "    AND vipPassword = ?";
            Object[] objects = new Object[3];
            objects[0] = allPrices;
            objects[1] = cId;
            objects[2] = vipPassword;
            num  = payCRUD.update(sql,objects);
            paid(cId);
        }

        return num;
    }

    //改变订单状态
    @Override
    public void paid(int cId) {
        String sql = "UPDATE orders\n" +
                "SET oStatus = '已支付'\n" +
                "WHERE cId = ? AND oStatus = '已下单，待支付'";
        Object[] objects = new Object[1];
        objects[0] = cId;
        payCRUD.update(sql,objects);
    }

    /*//取消订单
    //全部取消
    @Override
    public void allCancel() {
        String sql = "";
    }

    //部分取消
    @Override
    public void partCancel() {
        String sql = "";
    }*/




}
